import java.util.Arrays;
import java.util.*;

public class Text {
    public static void main(String[] args) {
    }
    public int[] intersect(int[] nums1, int[] nums2) {
        if(nums1.length > nums2.length) {
            return intersect(nums2,nums1);
        }
        int[] ret = new int[nums1.length];
        Map<Integer,Integer> hashmap = new HashMap<>();
        for(int i = 0; i < nums1.length; i++) {
            hashmap.put(nums1[i],hashmap.getOrDefault(nums1[i],0)+1);
        }
        int index = 0;
        for(int j = 0; j < nums2.length ;j++){
            if(hashmap.containsKey(nums2[j])) {
                int count = hashmap.get(nums2[j]);
                if(count > 0) {
                    ret[index++] = nums2[j];
                    count--;
                    if(count == 0){
                        hashmap.remove(nums2[j]);
                    }else {
                        hashmap.put(nums2[j],count);
                    }
                }
            }
        }
        return Arrays.copyOfRange(ret,0,index);
    }
    public int maxProfit(int[] prices) {
        int minprice = 10000;
        int maxprofit = 0;
        for(int i = 0; i < prices.length; i++) {
            if(prices[i] < minprice) {
                minprice = prices[i];
            }
            if(prices[i] - minprice > maxprofit) {
                maxprofit = prices[i] - minprice;
            }
        }
        return maxprofit;
    }
}
